package com.yutian.endecryption.mock.service;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;
import com.yutian.endecryption.mock.mapper.MockDataMapper;
import com.yutian.endecryption.mock.pojo.Examinee;
import com.yutian.endecryption.mock.pojo.ExaminerRecord;
import com.yutian.endecryption.mock.pojo.SubGroup;
import com.yutian.endecryption.mock.pojo.SubGroupDetail;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 根据界面身份证号码、姓名、考试车型在数据库植入考生伪数据
 *
 * @author zhang peng yang ming
 * @create 2017-08-01 14:00
 */
public class MockDataService extends BaseService<MockDataMapper> {
    /**
     * 调用其他service层相关函数
     *
     * @param idNum 身份证号码
     * @param name 考生姓名
     * @param carType 考试车辆类型
     * @return
     */
    public static int generateMockData (String idNum, String name, int carType) {
        Date date = new Date();
        int kskm = 3;
        int r = 0;
        // 创建考试员备案信息
        ExaminerRecord record = new ExaminerRecord();
        // xh, jbr, sfzmmc, sfzmhm, xm, fzrq, kszyxqz, cjsj, gxsj
        record.setXh("67888999");
        record.setJbr("洋明");
        record.setSfzmhm(Config.KSY_SFZMHM);
        record.setSfzmmc("A");
        record.setXm(Config.KSY);
        record.setFzrq(new Date(new Date().getTime() - (long)365 * 24 * 60 * 60 * 1000));
        record.setKszyxqz(new Date(new Date().getTime() + (long)365 * 24 * 60 * 60 * 1000));
        record.setCjsj(new Date(new Date().getTime() - (long)365 * 24 * 60 * 60 * 1000));
        record.setGxsj(new Date(new Date().getTime() - (long)365 * 24 * 60 * 60 * 1000));

        r = new ExaminerRecordService().createRecord(record);

        if (r != 0) {
            SubGroup subGroup = new SubGroup();
            //subGroup.setXh(("" + new Date().getTime()).substring(0, 11));

            subGroup.setXh(Config.XH);
            subGroup.setKskm(kskm);
            subGroup.setKsrq(date);
            subGroup.setKsdd("652819");
            subGroup.setKcxh("65000525");
            subGroup.setKsy(Config.KSY);
            subGroup.setGlbm("652800000400");
            r = new SubGroupService().createSubGroup(subGroup);
        }

        if (r != 0) {
            SubGroupDetail groupDetail = new SubGroupDetail();
            groupDetail.setSfzmhm(idNum);
            groupDetail.setXm(name);
            groupDetail.setDlr(Config.DLR);
            groupDetail.setXh(Config.XH);
            r = new SubGroupDetailService().createSubGroupDetail(groupDetail);
        }

        if (r != 0) {
            Examinee examinee = new Examinee();
            examinee.setKscx(carType == 0 ? "C1" : "C2");
            examinee.setXm(name);
            examinee.setSfzmhm(idNum);
            examinee.setYyrq(date);
            examinee.setYkrq(date);
            examinee.setKscc(1);
            examinee.setKsrq(date);
            examinee.setKscs(1);
            examinee.setKsy1(Config.KSY);
            examinee.setZt(0);
            examinee.setYycs(1);
            examinee.setBcyykscs(0);
            examinee.setCkyy(0);
            examinee.setKskm(kskm);
            examinee.setLsh("" + new Date().getTime());
            examinee.setZkzmbh(new SimpleDateFormat("yyyy").format(new Date()) + ("" + new Date().getTime()).substring(0, 8));
            examinee.setGlbm(new SimpleDateFormat("yyyy").format(new Date()) + "00000400");
            examinee.setKsdd("652819");

            r = new ExamineeService().createExaminee(examinee);
        }

        return r;
    }

    /**
     * 清除模拟考试相关的伪数据
     *
     * @return
     */
    public static int clearMockData() {
        return new MockDataService().clearMockData(Config.KSY_SFZMHM, Config.KSY);
    }

    /**
     * 根据考官身份证清除模拟考试相关的伪数据
     *
     * @param ksySfzmhm
     * @return
     */
    public int clearMockData(String ksySfzmhm, String ksy) {
        ksySfzmhm = SymmetricEncoder.AESEncode(Config.KEY, ksySfzmhm);
        ksy = SymmetricEncoder.AESEncode(Config.KEY, ksy);
        int r = 0;
        List<String> idNums = mapper(MockDataMapper.class).retrieveMockExamniee(ksy);
        for (String idNum : idNums) {
            r = mapper(MockDataMapper.class).deleteUplodeSignItem(idNum, SymmetricEncoder.AESEncode(Config.KEY, idNum));
            sessionClose();
        }
        r = mapper(MockDataMapper.class).delete(ksySfzmhm, ksy, Config.DLR);
        sessionClose();
        return r;
    }

    /**
     * 将所有模拟数据进行签到
     * @return
     */
    public static int signAll() {
        return new MockDataService().signInfo();
    }

    public int signInfo() {
        String img = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAEMuMjoyKkM6NjpLR0NPZKZsZFxcZMySmnmm8dT++u3U6eX//////////+Xp////////////////////////////2wBDAUdLS2RXZMRsbMT//+n/////////////////////////////////////////////////////////////////////wAARCABkAGQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwC/moZLqGPgtk+grNnuHlY/MQvYCoaANB9RH8CH8aga+mPQgfQVWooAmN1MeshphlkPV2/OmUqozH5VJ+lAAWLdST9aSpfs8gIBXaT0yalWwlPXaPxoAgWaRD8rkfjVyC/GMS5z6imf2dJ/eWg6fKOhU0AXkmjkHysDUlZD200XJU/UGpIL148K/wAy+/UUAadFNRw6hlOQaKAMi5Ty5mUdM8VFV/UYuBIPoagtbcztzwg60AQojOcKCT7Vbi09iMyNt9hV9I1jUKoAFOoAgjs4UH3cn1NTBQBgAClqK5l8qJm/AUAZt1J5k7EduBWjZlmt0LdfesuJTLKq+prZUBVAHQUALRRTXkSMZdgKAHVnaiiKysuAT1Ap81+BxEM+5qi7tIxZjkmgCSK5eNNqk4oojt3kXcBxRQBqyxiRCh6GorW2MBYl857VYooAKKKzHvZskcDn0oA06z9RlBIjHbk1XN1Of+Wh/CoSSTknJoAt2LRxlnkYA9BmrD6hGv3QWrMzRQBakvpX4XCj2qszMxyzEn3pKVVLHABNACU+ONpGCqMk1YhsXfBf5R+tX4YUhXCD6n1oAWGMRRBB2op9FABRRRQAVXntUlBwAretWKDQBiSxNE5VqaFJ6AmprqQyznHQcCtG1i8uBQRz1NAGQVI6g0lbpVWGGUH8Khezhb+AD6UAUbUW5P73OffpWlGiAfIFA9qoy2DrzGwYehqCOaW3OASMdjQBsUVBb3KzDHRh2qegAooooAKKKKACoLuTy4CQeTwKnrM1CTdNtB4X+dAEdpH5s49Bya1xVDTmRQ2Thj61ezQAtFBIFQy3UUfBbJ9BQBNVHUXj2BeC+fyqKa+d+I/lH61UJJOScmgByOUcMpwRWzC4kiVh3FYlaenkmDB6A8UAW6KKKACiiigBsjBI2Y9hWIxLMT1JNaWokiEAdCearWUReYMR8q0AVeRTg7DoxH41evLXdmSMc9x61nkYOCKAFLserE/jSUUUAFFFTQ20kpGBgepoAjjRpHCqMk1swxiKIIO1Mgt0hGByfU1NQAUUUUAFFFFACMoYYYZB7U0RIEKBcA+lFFAChFAxVe6t4zGX2/N60UUAZqnBNWbZRNJtcce1FFAFwW0S9Fp5jXA6/nRRQA7aPU/nSFBkcn86KKAHBcdz+dFFFAH/2Q==";
        String ksy = SymmetricEncoder.AESEncode(Config.KEY, Config.KSY);
        List<String> sfzmhms = mapper(MockDataMapper.class).getAllMockExamineeId(ksy);
        System.out.println(sfzmhms);
        sessionClose();

        if (sfzmhms.size() != 0) {
            mapper(MockDataMapper.class).deleteSifnInfo(sfzmhms);
            sessionClose();
        }
        int r = 0;
        for (String sfzmhm : sfzmhms) {
             r = mapper(MockDataMapper.class).insertSignInfo(img, img, sfzmhm, new Date());
            sessionClose();
        }

        return sfzmhms.size();
    }
}
